package com.jieyang.oa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jieyang.oa.dao.DutyDao;
import com.jieyang.oa.pojo.entity.Duty;
import com.jieyang.oa.pojo.Vo.DutyVo;
import com.jieyang.oa.service.DutyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 29276
 * @version 1.0
 * @description: TODO
 * @date 2021/8/20 20:16
 */
@Service
@Transactional
public class DutyServiceImpl implements DutyService {

    @Resource
    private DutyDao dutyDao;

    @Override
    public Integer signIn(String empId) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        String dutyDate = String.valueOf(java.sql.Date.valueOf(dateStr));
        Duty isDuty = dutyDao.selectByEmpIdDate(empId, dutyDate);
        if (isDuty == null) {
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            String format = sdfTime.format(date);
            String signInTime = String.valueOf(Time.valueOf(format));
            Duty duty = new Duty();
            duty.setEmpId(empId);
            duty.setDutyDate(dutyDate);
            duty.setSignInTime(signInTime);
            Integer integer = dutyDao.insertDuty(duty);
            if (integer == 1) {
                return 1;
            }
            return 2;
        }
        return 0;
    }

    @Override
    public Integer signOut(String empId) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        String dutyDate = String.valueOf(java.sql.Date.valueOf(dateStr));
        Duty duty = dutyDao.selectByEmpIdDate(empId, dutyDate);
        if (duty != null) {
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            String format = sdfTime.format(date);
            String signOutTime = String.valueOf(Time.valueOf(format));
            duty.setSignOutTime(signOutTime);
            Integer integer = dutyDao.updateDuty(duty);
            if (integer == 1) {
                return 1;
            }
            return 2;
        }
        return 0;
    }

    @Override
    public PageInfo<DutyVo> selectAll(int pageNum, int pageSize, DutyVo dutyVo) {
        PageHelper.startPage(pageNum, pageSize);

        List<DutyVo> dutyVos = dutyDao.selectAll(dutyVo);
        PageInfo<DutyVo> dutyVoPageInfo = new PageInfo<>(dutyVos);
        int ceil = (int) Math.ceil((dutyDao.count(dutyVo) * 1.0) / pageSize);
        dutyVoPageInfo.setPages(ceil);
        dutyVoPageInfo.setSize(dutyDao.count(dutyVo));
        return dutyVoPageInfo;
    }

    @Override
    public PageInfo<Duty> selectByEmpId(String empId, int pNum, int pSize) {
        PageHelper.startPage(pNum, pSize);

        List<Duty> duties = dutyDao.selectByEmpId(empId);

        PageInfo<Duty> dutyPageInfo = new PageInfo<>(duties);
        int ceil = (int) Math.ceil((dutyDao.dutyCount(empId)*1.0)/pSize);
        dutyPageInfo.setPages(ceil);
        dutyPageInfo.setSize(dutyDao.dutyCount(empId));
        return dutyPageInfo;
    }
}
